package com.mchsdk.open;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.mchsdk.paysdk.bean.FlagControl;
import com.mchsdk.paysdk.utils.MCLog;

/**
 * Created by zhujinzhujin on 2017/1/5.
 */

public class MCHBaseActivity extends Activity {
    private final static String TAG = "MCHBaseActivity";

    HomeKeyEventBroadCastReceiver receiver;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerBroadCast();
    }

    /**
     * 注册广播
     */
    private void registerBroadCast() {
        receiver = new HomeKeyEventBroadCastReceiver();
        registerReceiver(receiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
    }

    @Override
    protected void onStart() {
        MCLog.d(TAG, "onStart startFloating");
        MCApiFactory.getMCApi().startFloating(MCHBaseActivity.this);

        super.onStart();
    }
    @Override
    protected void onRestart() {
        MCLog.d(TAG, "onRestart startFloating");
        MCApiFactory.getMCApi().startFloating(MCHBaseActivity.this);
        super.onRestart();
    }
    @Override
    protected void onResume() {
        MCLog.d(TAG, "onResume startFloating");
        MCApiFactory.getMCApi().startFloating(MCHBaseActivity.this);
        if(FlagControl.isLogin&&!FlagControl.isStart)
        {
            MCApiFactory.getMCApi().requestAntiAddiction();
        }
        super.onResume();
    }

    @Override
    protected void onPause() {
        MCLog.d(TAG, "onPause startFloating");
        MCApiFactory.getMCApi().startFloating(MCHBaseActivity.this);
//        if(FlagControl.isLogin&&FlagControl.isStart&&FlagControl.isOffLine)
//        {
//            MCApiFactory.getMCApi().offLineAnnounce(MCHBaseActivity.this);
//            FlagControl.isStart=false;
//            FlagControl.isOffLine=false;
//        }
        super.onPause();
    }

    @Override
    protected void onStop() {
        MCLog.d(TAG, "onStop stopFloating");
        MCApiFactory.getMCApi().stopFloating(MCHBaseActivity.this);
        super.onStop();
    }
    @Override
    protected void onDestroy() {
        MCLog.d(TAG, "onDestroy stopFloating");
        MCApiFactory.getMCApi().stopFloating(MCHBaseActivity.this);
        if(FlagControl.isLogin&&FlagControl.isStart)
        {
            MCApiFactory.getMCApi().offLineAnnounce(MCHBaseActivity.this);
        }
        unregisterReceiver(receiver);
        super.onDestroy();
    }


    /**
            * home键监听广播
     */
    class HomeKeyEventBroadCastReceiver extends BroadcastReceiver {

        static final String SYSTEM_REASON = "reason";
        static final String SYSTEM_HOME_KEY = "homekey";//home key
        static final String SYSTEM_RECENT_APPS = "recentapps";//long home key

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                String reason = intent.getStringExtra(SYSTEM_REASON);
                if (reason != null) {
                    if (reason.equals(SYSTEM_HOME_KEY)) {
                        // home key处理点
                        if(FlagControl.isLogin&&FlagControl.isStart)
                        {
                            MCApiFactory.getMCApi().offLineAnnounce(MCHBaseActivity.this);
                        }
                    } else if (reason.equals(SYSTEM_RECENT_APPS)) {
                        // long home key处理点
                    }
                }
            }
        }
    }
}
